<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        table {
            width: 500px;
            margin: 100px auto;
            margin-top: 0;
            border-collapse: collapse;
            text-align: center;
        }

        td,
        th {
            border: 1px solid #333;
        }

        thead tr {
            height: 40px;
            background-color: #ccc;
        }

        tbody tr td:last-child a {
            text-decoration: none;
            color: green;
        }

        div {
            width: 500px;
            margin: 0 auto;
        }

        div input {
            width: 70px;
        }

        tbody tr td a:first-child {
            color: red;
            margin-right: 10px;
        }

        tbody input {
            width: 70px;
        }
    </style>
</head>

<body>
    <div>
        姓名：<input type="text">
        考试科目：<input type="text">
        成绩：<input type="text">
        <button>添加学员信息</button>
    </div>
    <table cellspacing="0">
        <thead>
            <tr>
                <th>姓名</th>
                <th>科目</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</body>
<script>
    var t_body = document.querySelector('tbody');
    var ips = document.querySelectorAll('div input');
    var btn = document.querySelector('button');
    btn.addEventListener('click', function () {
        if (ips[0].value == '') {
            return alert(`姓名不能为空!`)
        } else if (ips[1].value == '') {
            return alert(`科目不能为空!`)
        } else if (ips[2].value == '') {
            return alert(`成绩不能为空!`)
        } else {
            var tr = document.createElement('tr')
            t_body.appendChild(tr);
            for (var i = 0; i < ips.length; i++) {
                var td = document.createElement('td');
                td.innerHTML = ips[i].value;
                tr.appendChild(td);
                ips[i].value = '';
            }
            var td1 = document.createElement('td')
            td1.innerHTML = `<a href="javascript:;">删除</a><a href="javascript:;">编辑</a>`;
            tr.appendChild(td1);
            var temp = '';
            td1.addEventListener('click', function (e) {
                if (e.target.innerText == '删除') {
                    e.target.parentNode.parentNode.remove();
                }
                else if (e.target.innerText == '编辑') {
                    var tds = e.target.parentNode.parentNode.children[2]
                   if(tds.children.length<1){
                    
                    temp = tds.innerHTML;
                    tds.innerHTML = `<input type='text' value='${temp}'><button id='bc'>保存</button><button id='qx'>取消</button>`
                    var bc = document.querySelector('#bc')
                    var qx = document.querySelector('#qx')
                    bc.addEventListener('click', function () {
                        tds.innerHTML = this.parentNode.children[0].value;
                    })
                    qx.addEventListener('click', function () {
                        tds.innerHTML = temp;
                    })
                   }else{
                       tds.innerHTML=temp
                   }
                }
            })
        }
    })
</script>

</html>